load("@rules_gazebo//gazebo:headers.bzl", "gz_configure_header", "gz_export_header")
load("@rules_license//rules:license.bzl", "license")

package(
    default_applicable_licenses = [":license"],
    features = [
        "layering_check",
        "parse_headers",
    ],
)

license(
    name = "license",
    package_name = "gz-common",
)

licenses(["notice"])

exports_files([
    "package.xml",
    "LICENSE",
    "MODULE.bazel",
])

gz_configure_header(
    name = "Config",
    src = "include/gz/common/config.hh.in",
    package_xml = "package.xml",
)

gz_export_header(
    name = "Export",
    out = "include/gz/common/Export.hh",
    export_base = "GZ_COMMON",
    lib_name = "gz-common",
)

public_headers_no_gen = glob([
    "include/gz/common/*.hh",
    "include/gz/common/detail/*.hh",
])

private_headers = glob(
    include = [
        "src/*.hh",
    ],
)

sources = glob(
    include = [
        "src/*.cc",
    ],
    exclude = [
        "src/Plugin.cc",
        "src/PluginLoader.cc",
        "src/*_TEST.cc",
    ],
)

public_headers = public_headers_no_gen + [
    "include/gz/common/config.hh",
    "include/gz/common/Export.hh",
]

cc_library(
    name = "gz-common",
    srcs = sources + private_headers,
    hdrs = public_headers,
    copts = [
        "-fexceptions",
        "-Wno-unused-value",
    ],
    includes = ["include"],
    visibility = ["//visibility:public"],
    deps = [
        "@gz-math",
        "@gz-utils//:ImplPtr",
        "@gz-utils//:NeverDestroyed",
        "@gz-utils//:SuppressWarning",
        "@gz-utils//log:Logger",
        "@libuuid",
        "@spdlog",
    ],
)

test_sources = glob(
    include = ["src/*_TEST.cc"],
    exclude = [
        "src/PluginUtils_TEST.cc",
        "src/PluginLoader_TEST.cc",
    ],
)

[cc_test(
    name = src.replace("/", "_").replace(".cc", "").replace("src_", ""),
    srcs = [src],
    copts = ["-fexceptions"],
    deps = [
        ":gz-common",
        "//testing",
        "@googletest//:gtest",
        "@googletest//:gtest_main",
        "@gz-math",
        "@gz-utils//:ExtraTestMacros",
    ],
) for src in test_sources]
